require.config({
	baseUrl: "",
	paths: {
		"director": 'ComponentsJS/director',
		"jquery": 'ComponentsJS/jquery',
		"sinaEmotion": 'ComponentsJS/jquery.sinaEmotion',
		"underscore": 'ComponentsJS/underscore-min',
		"backbone":"ComponentsJS/backbone",
		'swiper':"ComponentsJS/swiper.min",
		"blog": 'module_blog/blog',
		"dateHappy": "module_dateHappy/dateHappy",
		'datePlugin':'module_accounts/datePlugin',        //账本页面的日期加载器
		"accounts":'module_accounts/accounts',
		"bb_todos":'module_todos/bb_todos',
		"todos":'module_todos/todos'
	}
});

require(['director', 'jquery', 'sinaEmotion', 'underscore','backbone','swiper','blog', 'dateHappy', 'datePlugin','accounts','bb_todos',"todos"], function(director, $, sinaEmotion, _,backbone,swiper,blog, dateHappy, datePlugin, accounts, app,todos) {
	var index = {
		initialize: function() {
			index.setRoute();
			index.onShow();
			index.bindEvent();
			index.util_fn();
		},
		
		onShow:function(){
			if(parseInt($('body').css('width'))<=640){
				$('#nav .nav_ul').hide();
				$('.menu').show();
			}else{
				$('.menu').hide();
				$('#nav .nav_ul').show();				
			}
		},
		
		bindEvent:function(){
			$('.menu_btn').on('click',function(){
				index.trans_menu();
			});
			$('.mask').on('click',function(){
				$(this).hide();
				$('.menu_content').hide();
			});
			$('.menu_content_li').on('click',function(){
				$(this).closest('.menu_content').hide();
				$('.mask').hide();
			});
		},
		
		trans_menu:function(){
			$('.menu_content').toggle();
            if($('.menu_content').css('display')=='block'){
            	$('.mask').show();
            }
		},
		
		setRoute: function() {
			var route = {
				'/tab_blog': set_blog,
				'/tab_dateHappy': set_dateHappy,
				'/tab_accounts':set_accounts,
				'/tab_todos':set_todos
			}

			var router = Router(route);
			router.init();

			function set_blog() {

//				$.ajaxSetup({
//					cache: false //close AJAX cache
//				});
//				$('#contain').load('module_blog/blog.html');   //会有缓存，所以会只load一次，清空缓存加载时会找不到元素
				$.ajax({
					type: "get",
					url: "module_blog/blog.html",
					async: true,
					success: function(data) {
						$('#contain').html(data);
						blog.initialize();
					}
				});
				
				

			}

			function set_dateHappy() {
                
                $.ajax({
					type: "get",
					url: "module_dateHappy/dateHappy.html",
					async: true,
					success: function(data) {
						$('#contain').html(data);
						dateHappy.initialize();
					}
				});
				
				
			}
			
			function set_accounts(){
				$.ajax({
					type: "get",
					url: "module_accounts/accounts.html",
					async: true,
					success: function(data) {
						$('#contain').html(data);
						accounts.initialize();     //ajax是异步的，不能把这句放到success外面
					}
				});
				
				
			}
			
			function set_todos(){
				$.ajax({
					type:"get",
					url:"module_todos/todos.html",
					async:true,
					success:function(data){
						$('#contain').html(data);
                        //var myView = new app.appView();
                        todos.initialize();
					}
				});
			}
			
		},
		
		//这里执行一次
		util_fn: function() {
			Date.prototype.format = function(format) {
				var o = {
					"M+": this.getMonth() + 1, //month
					"d+": this.getDate(), //day
					"h+": this.getHours(), //hour
					"m+": this.getMinutes(), //minute
					"s+": this.getSeconds(), //second
					"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
					"S": this.getMilliseconds() //millisecond
				}
				if(/(y+)/.test(format)) {
					format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				}
				for(var k in o) {
					if(new RegExp("(" + k + ")").test(format)) {
						format = format.replace(RegExp.$1,
							RegExp.$1.length == 1 ? o[k] :
							("00" + o[k]).substr(("" + o[k]).length));
					}
				}
				return format;
			}

			Array.prototype.removeIndex = function(index) {
				if(index <= (this.length - 1)) {
					for(var i = index; i < this.length; i++) {
						this[i] = this[i + 1];
					}
				} else {
					throw new Error('超出最大索引');
				}
				this.length = this.length - 1;
				return this;
			}

		}

	}
	index.initialize();
});